<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/03/0003
 * Time: 14:05
 */

namespace app\sports_admin\controller;


use app\exception\SchoolException;
use app\sports_admin\model\Region;
use think\Request;
use app\sports_admin\model\School as SchoolModel;
use app\sports_admin\service\School as SchoolService;
use app\sports_admin\vaildate\School as SchoolVaildate;
use app\sports_admin\model\Grade as GradeModel;
class School extends Base
{
    protected $beforeActionList = [
        'isUsers'=>['only'=>'index,schoollist,add,regionfind,schoolinsert,edit,schoolupdate,schooldelete,resetpass'],
        'operationSelect'=>['only'=>'index,schoollist,add,regionfind,edit'],
        'operationUpdate'=>['only'=>'schoolupdate,resetpass'],
        'operationInsert'=>['only'=>'schoolinsert'],
        'operationDelete'=>['only'=>'schooldelete']
    ];
    //学校信息首页
    public function index()
    {
        return view('index');
    }

    /**
     * 获取学校信息列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
    */
    public function schoolList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'school_id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $result = SchoolModel::getSchoolInfo($page,$rows,$sort,$sortOrder,$searchName);
        return $result;
    }
    /**
     * 添加学校页面
    */
    public function add()
    {
        $type = GradeModel::group('type')->select();
        return view('add',compact('type'));
    }
    /**
     * 城市级联操作
     * @param $id 父id
     */
    public function regionFind(){
        $id = input('get.id');
        $data = Region::where("parent_id={$id}")->select();
        return json($data);
    }
    /**
     * 添加学校
    */
    public function schoolInsert(Request $request)
    {
        if($request->isAjax()){
            (new SchoolVaildate())->insert();
            return SchoolService::schoolInsert();
        }
    }
    /**
     * 编辑学校
     * @param  $id 上学信息id
    */
    public function edit()
    {
        $id = input('get.id');
        $info = SchoolModel::schoolInfo($id);
        $type = GradeModel::group('type')->select();
        return view('edit',compact('info','type'));
    }
    /**
     * 修改学校信息
    */
    public function schoolUpdate(Request $request)
    {
        if($request->isAjax()){
            (new SchoolVaildate())->update();
            return SchoolService::schoolUpdate();
        }
    }
    /**
     * 删除学校
     * @param $id 学校id
    */
    public function schoolDelete(Request $request)
    {
        if($request->isAjax()){
            (new SchoolVaildate())->go_check();
            $id = $request->post('id');
            $bool = SchoolModel::destroy($id);
            if(!$bool){
                throw new SchoolException([
                    'errorCode'=>203,
                    'msg'=>'删除失败'
                ]);
            }
            return json(['code'=>200,'msg'=>'删除成功']);
        }
    }
    /**
     * 学校密码重置
     * @param $id 学校id
    */
    public function resetPass(Request $request)
    {
        if($request->isAjax()){
            $id = $request->post('school_id');
            return SchoolService::resetPass($id);
        }
    }
}